<?php
declare (strict_types = 1);

namespace app\controller\backend\user;

use app\service\logic\common\chat\FriendsExpertService;
use app\service\logic\common\user\UserService;
use app\validate\common\user\UserValidate;
use think\App;
use think\Request;
use think\Response;
use think\facade\View;
use think\response\Json;
use think\db\exception\DbException;
use app\controller\backend\BackendController;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

class UserController extends BackendController
{
    protected $service;

    public function __construct(App $app, UserService $service)
    {
        parent::__construct($app);

        $this->service = $service;
    }

    /**
     * 显示资源列表
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);
        $this->validate($params, UserValidate::class . '.index');

        $result = $this->service->getList($params, $search_fields, $search);
        return amis($result);
    }

    /**
     * 下拉菜单
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function options()
    {
        $params = $this->request->param();
        $search_fields = $this->service->getSearchColumn();
        $search = request()->only($search_fields);

        $this->validate($params, UserValidate::class . '.options');

        $drop = $this->service->getOptions($params, $search_fields, $search);

        return amis(['options' => $drop]);
    }

    /**
     * 保存新建的资源
     *
     * @param Request $request
     * @return Response
     */
    public function save(Request $request)
    {
        $params = $request->only($this->service->getSaveColumn());
        $this->validate($params, UserValidate::class . '.save');
        $params['admin_id'] = $this->request->middleware('admin')['id'];

        $result = $this->service->create($params);
        return amis($result);
    }

    /**
     * 用户信息
     *
     * @return Json
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     */
    public function read()
    {
        $params = $this->request->param();
        $this->validate($params, UserValidate::class . '.id');

        $result = $this->service->getSimpleDetail($params);
        return amis($result);
    }

    /**
     * @return Json
     */
    public function experts()
    {
        $params = $this->request->param();
        $this->validate($params, UserValidate::class . '.id');

        $fun = function ($model) {
            unset($model['belongExperts']);
            return $model;
        };
        // 无分页吧
        $result = invoke(FriendsExpertService::class)->getListOrderByUpdateAt($params, ['user_id'], ['user_id' => $params['id']], ['belongExperts'], $fun, false);
        return amis($result);
    }
}
